package com.kasax.plugin.app.visitor.log

import com.kasax.plugin.app.LogUtils
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.Opcodes

/**
 * 需结合ClassVisitor遍历方法，检测@ASMTest注解，构造MethodVisitor传递needInject标志。
 visitCode方法在方法体开始时调用，插入打印指令即可。
 */
class LogMethodVisitor extends MethodVisitor {
    private String TAG = "LogMethodVisitor"

    LogMethodVisitor(MethodVisitor methodVisitor) {
        super(Opcodes.ASM7, methodVisitor)
    }

    @Override
    void visitCode() {
        super.visitCode()
        LogUtils.d(TAG, "visitCode start")
        // 插入打印指令
        mv.visitLdcInsn("ASM_CUSTOM_LOG")
        mv.visitLdcInsn("custom transform log")
        mv.visitMethodInsn(
                Opcodes.INVOKESTATIC,
                "android/util/Log",
                "e",
                "(Ljava/lang/String;Ljava/lang/String;)I",
                false
        )
        mv.visitInsn(Opcodes.POP)
        LogUtils.d(TAG, "visitCode complete")
    }
}